package org.zht.dormpro.auth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.CollectionUtil;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.pub.constant.Enums;
import org.zht.dormpro.auth.mapper.SysRoleMapper;
import org.zht.dormpro.auth.mapper.SysRolePermissionMapper;
import org.zht.dormpro.auth.model.SysRole;
import org.zht.dormpro.auth.model.SysRolePermission;
import org.zht.dormpro.auth.service.IRoleService;
import org.zht.dormpro.auth.service.ISysUserService;
import org.zht.dormpro.pub.util.ShiroUtil;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    private ISysUserService sysUserService;


    @Override
    public Integer addRole(SysRole sysRole) {
        //设置其他数据
        sysRole.setCreateTime(System.currentTimeMillis());
        sysRole.setMender(1);
        sysRole.setCreator(1);
        sysRole.setModifyTime(System.currentTimeMillis());
        int i = sysRoleMapper.insertSelective(sysRole);
        if (i > 0) {
            return sysRole.getId();
        }
        return i;
    }

    @Override
    public boolean addRolePermission(List<String> menuIds, Integer roleId) {
        //去除-1
        if (CollectionUtil.isNotEmpty(menuIds)) {
            boolean a = menuIds.remove("-1");
            SysRolePermission sysRolePermission = null;
            for (String menuId : menuIds) {
                sysRolePermission = new SysRolePermission();
                sysRolePermission.setAvailable(1);
                sysRolePermission.setCreateTime(System.currentTimeMillis());
                sysRolePermission.setCreator(0);
                sysRolePermission.setMender(0);
                sysRolePermission.setModifyTime(System.currentTimeMillis());
                sysRolePermission.setSysRoleId(roleId);
                sysRolePermission.setSysPermissionId(Integer.valueOf(menuId));
                sysRolePermissionMapper.insertSelective(sysRolePermission);
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean updateRolePermission(List<String> menuIds, Integer roleId) {
        //移除所有的
        boolean b = deleteMenuPerRL(roleId, Enums.MRStatus.ROLE);
        //去除-1
        if (CollectionUtil.isNotEmpty(menuIds) & b) {
            boolean a = menuIds.remove("-1");
            SysRolePermission sysRolePermission = null;
            for (String menuId : menuIds) {
                sysRolePermission = new SysRolePermission();
                sysRolePermission.setAvailable(1);
                sysRolePermission.setCreateTime(System.currentTimeMillis());
                sysRolePermission.setCreator(0);
                sysRolePermission.setSysRoleId(roleId);
                sysRolePermission.setSysPermissionId(Integer.valueOf(menuId));
                sysRolePermission.setMender(ShiroUtil.getAuthUser().getUserId());
                sysRolePermission.setModifyTime(System.currentTimeMillis());
                sysRolePermissionMapper.insertSelective(sysRolePermission);
            }
            return true;
        }
        return b;
    }


    /**
     * 删除菜单权限/角色权限之间关联关系
     *
     * @return
     */
    @Override
    public boolean deleteMenuPerRL(Integer id, Enums.MRStatus mrStatus) {
        int i = sysRolePermissionMapper.deleteMenuPerRL(id, mrStatus.toString());
        return i >= 0;
    }

    //通过参数进行查询
    @Override
    public List<SysRole> getRoleByCondition(PageBean pageBean) {
        Page<Object> objects = PageHelper.startPage(pageBean.getPageNum(), pageBean.getLimit());
        List<SysRole> roles = sysRoleMapper.getAllRoleByCondition(pageBean);
        pageBean.setTotal((int) objects.getTotal());
        return roles;
    }

    //删除角色
    @Override
    public boolean deleteRole(@NonNull Integer id) {
        int key = sysRoleMapper.deleteByPrimaryKey(id);
        if (key > 0) {
            //删除角色权限关联
            boolean a = deleteMenuPerRL(id, Enums.MRStatus.ROLE);
            //删除用户角色关联
            boolean b = sysUserService.deleteUserRoRL(id, Enums.URStatus.ROLE);
            if (a & b) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean batchRemoveRole(List<String> roleIds) {
        if (CollectionUtil.isNotEmpty(roleIds)) {
            for (String roleId : roleIds) {
                boolean b = deleteRole(Integer.valueOf(roleId));
                if (!b) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public List<SysRolePermission> getPermissionByRoleId(Integer id, Enums.MRStatus mrStatus) {
        List<SysRolePermission> sysRolePermissions = sysRolePermissionMapper.getPermissionRoleRL(id,mrStatus.toString());
        return sysRolePermissions;
    }

    @Override
    public SysRole getRoleById(Integer roleId) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(roleId);
        return sysRole;
    }

    @Override
    public boolean updateRole(SysRole sysRole) {
        sysRole.setMender(ShiroUtil.getAuthUser().getUserId());
        sysRole.setModifyTime(System.currentTimeMillis());
        int i = sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        return i > 0;
    }

    @Override
    public List<SysRole> getAllRole(int all) {
        List<SysRole> roles = sysRoleMapper.getAllRole(all);
        return roles;
    }
}
